import { errorMapper } from './tools/errorMapper'

import gameControl from './modules/gameControl';


import eventManager from './modules/event/eventManager';
import { eventMap } from './modules/event/eventMap';
import taskManager from './modules/task/taskManager';
import { taskType } from './enum';
import { roomOwner } from './tools/baseTools';
// eventManager.add("测试", 10, eventMap.sayHellow)    // 测试事件中心

gameControl.init()
export const loop = errorMapper(() => {
    isRestart()
    gameControl.run()   // 运行主程序
    Object.keys(Game.rooms).map(roomName => {
        let room = Game.rooms[roomName]
        if (roomOwner(room) != 0) return
        if (!room.memory.autoBuild.structure)return
        if (!room.memory.autoBuild.structure.road)return
        Object.keys(room.memory.autoBuild.structure.road).map(id => {
            let path = Room.deserializePath(room.memory.autoBuild.structure.road[id].path)
            path = path.map(pos => { return { 'x': pos.x, 'y': pos.y } })
            room.visual.poly(path, { stroke: 'red' })
        })
    })
    // #region 任务管理器
    // let taskID = '测试' + Game.time
    // let taskDeta = {
    //     taskID: taskID,
    //     taskType: taskType.spawnCreep,
    //     publisherID: '321' + Game.time,
    //     roomName: 'W8N7'
    // }
    // taskManager.addTask(taskDeta)
    // taskManager.getTaskByType(taskType.spawnCreep, '锤子测试用', 'W8N7', true)
    // taskManager.finishedTask("第" + Game.time + "Tick: " + taskID)
    // #endregion
})

function isRestart() {
    if (!Memory.gameControl) {
        Memory.gameControl = {}
        Memory.gameControl.spawnID = Game.spawns[Object.keys(Game.spawns)[0]].id
    }
    if (Object.keys(Game.spawns).length == 1 && Memory.gameControl.spawnID != Game.spawns[Object.keys(Game.spawns)[0]].id) {
        console.log(`${Date()} :重新生成spawn`);

        gameControl.init()
        Memory.gameControl.spawnID = Game.spawns[Object.keys(Game.spawns)[0]].id
    }
}